package com.xinchen.aiowaste.ui.activity

import android.os.Bundle
import android.text.TextUtils
import android.widget.LinearLayout
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.maple.common.widget.view.PaginationLayout
import com.maple.model.entity.aio.WasteListEntity
import com.maple.utils.const.ConstKey
import com.maple.utils.ext.toGone
import com.maple.utils.ext.toVisible
import com.maple.utils.util.LogUtils
import com.xinchen.aiowaste.R
import com.xinchen.aiowaste.base.BaseActivity
import com.xinchen.aiowaste.ui.adapter.WasteListAdapter
import com.xinchen.aiowaste.vm.WasteListViewModel
import java.util.WeakHashMap

class WasteListActivity : BaseActivity() {

    private var type: String = TYPE_IN_DANGEROUS
    companion object {
        const val TYPE_IN_DANGEROUS: String = "inDangerous"
        const val TYPE_IN_INDUSTRY: String = "inIndustry"
        const val TYPE_OUT_DANGEROUS: String = "outDangerous"
        const val TYPE_OUT_INDUSTRY: String = "outIndustry"
    }

    private var llEmpty: LinearLayout? = null
    private var rvList: RecyclerView? = null
    private var pagination: PaginationLayout? = null

    private var attribute: String = ""

    private val viewModel by viewModels<WasteListViewModel>()


    private val listAdapter: WasteListAdapter by lazy {
        WasteListAdapter(this).apply {
            this.setData(arrayListOf())
            this.setListener(object :WasteListAdapter.OnClickListener{
                override fun onItemClick(pos: Int, item: WasteListEntity.DataList?) {
                    item?.let {
                        val bundle: Bundle = Bundle()
                        bundle.putString(ConstKey.BundleKey.EXTRA_TYPE,type)
                        bundle.putSerializable(ConstKey.BundleKey.EXTRA_OBJ,it)
                        onStartActivity(InboundWeightActivity::class.java, bundle = bundle)
                    }
                }
            })
        }
    }
    override fun getLayoutId(): Int = R.layout.activity_waste_list

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        showClockTime()
        onBackClick()
        llEmpty = findViewById(R.id.ll_empty)
        llEmpty?.setOnClickListener {
            val params: WeakHashMap<String, Any> = WeakHashMap()
            params.put("entId", viewModel.entId)
            params.put("attribute", attribute)
            params.put("start", pagination?.getIndex().toString())
            params.put("limit", pagination?.getLimit().toString())
            viewModel.getWasteList(params,true)
        }
        rvList = findViewById(R.id.rv_list)
        rvList?.let {
            it.layoutManager = GridLayoutManager(this,4)
            it.adapter = listAdapter
        }
        pagination = findViewById(R.id.pagination)
        pagination?.reset(0,8)
        pagination?.setOnPaginationListener(object :PaginationLayout.OnPaginationListener{
            override fun onStartPage(start: Long, limit: Long) {
                val params: WeakHashMap<String, Any> = WeakHashMap()
                params.put("entId", viewModel.entId)
                params.put("attribute", attribute)
                params.put("start", start.toString())
                params.put("limit", limit.toString())
                viewModel.getWasteList(params)
            }

            override fun onPrevPage(start: Long, limit: Long) {
                val params: WeakHashMap<String, Any> = WeakHashMap()
                params.put("entId", viewModel.entId)
                params.put("attribute", attribute)
                params.put("start", start.toString())
                params.put("limit", limit.toString())
                viewModel.getWasteList(params)
            }

            override fun onNextPage(start: Long, limit: Long) {
                val params: WeakHashMap<String, Any> = WeakHashMap()
                params.put("entId", viewModel.entId)
                params.put("attribute", attribute)
                params.put("start", start.toString())
                params.put("limit", limit.toString())
                viewModel.getWasteList(params)
            }

            override fun onEndPage(start: Long, limit: Long) {
                val params: WeakHashMap<String, Any> = WeakHashMap()
                params.put("entId", viewModel.entId)
                params.put("attribute", attribute)
                params.put("start", start.toString())
                params.put("limit", limit.toString())
                viewModel.getWasteList(params)
            }
        })
    }
    override fun initData(savedInstanceState: Bundle?) {
        viewModel.defUI.showDialog.observe(this, Observer {
            showLoading()
        })

        viewModel.defUI.dismissDialog.observe(this, Observer {
            dismissLoading()
        })

        viewModel.defUI.toastEvent.observe(this, Observer {
            showToast(it)
        })

        viewModel.defUI.emptyEvent.observe(this, Observer{
            rvList?.toGone()
            pagination?.toGone()
            llEmpty?.toVisible()
        })

        viewModel.wasteData.observe(this, Observer{
            LogUtils.logGGQ("====list====:${it.list.size}")
            pagination?.setPageData(it.index,it.count,it.size)

            if(!it.list.isNullOrEmpty()) {
                llEmpty?.toGone()
                rvList?.toVisible()
                pagination?.toVisible()
                listAdapter.setData(it.list)
            }
        })


        type = getBundle()?.getString(ConstKey.BundleKey.EXTRA_TYPE).toString()
        if(TextUtils.equals(type,TYPE_IN_DANGEROUS) || TextUtils.equals(type,TYPE_OUT_DANGEROUS)) {
            setTitle("选择危险废物")
            attribute = "2"
            val params: WeakHashMap<String, Any> = WeakHashMap()
            // attribute 是 string 废物类别，2：危险废物，1：一般固废
            params.put("entId", viewModel.entId)
            params.put("attribute", attribute)
            params.put("start", pagination?.getIndex().toString())
            params.put("limit", pagination?.getLimit().toString())
            viewModel.getWasteList(params,true)
        } else if(TextUtils.equals(type, TYPE_IN_INDUSTRY) || TextUtils.equals(type, TYPE_OUT_INDUSTRY)) {
            setTitle("一般工业废物")
            attribute = "1"
            val params: WeakHashMap<String, Any> = WeakHashMap()
            params.put("entId", viewModel.entId)
            params.put("attribute", attribute)
            params.put("start", pagination?.getIndex().toString())
            params.put("limit", pagination?.getLimit().toString())
            viewModel.getWasteList(params,true)
        }
    }
}